<template>
    <div id="DismissClassContainer">
        <div id="headTitle">解散班级</div>
        <div id="classInfoContainer">
            <div id="head">班级信息</div>
            <div id="classInfo">
                <div class="rowInfo">班级ID：{{ userClassInfo.classID }}</div>
                <div class="rowInfo">班级：{{ userClassInfo.className }}</div>
                <div class="rowInfo">班级人数：{{ userClassInfo.classSize }}</div>
                <div class="rowLastInfo">班费余额：{{ userClassInfo.classExBala }}</div>
                <el-button class="rowInfo" id="button" type="primary" @click="dismissClass">
                    解散班级
                </el-button>
            </div>
        </div>
    </div>
</template>

<script>
//css
import "../assets/DismissClass.css";
//请求
import request from '@/utils/requset.js';
//顶部提示信息
import prompt from '@/utils/prompt.js'
//路由跳转
import router from "@/router";

export default {
    name: 'classInfo',
    data() {
        return {
            userClassInfo: {
                classID: "",
                className: "",
                classSize: "",
                classExBala: ""
            },
            dismissClassData: {}
        }
    },
    created() {
        if (this.isIntoClass() && this.isClassAdmin()) {
            this.loadClassData();
        }
    },
    methods: {
        isIntoClass() {
            let classID = localStorage.getItem("userInfo") ? JSON.parse(localStorage.getItem("userInfo")).classID : 0;
            if (classID < 1) {
                prompt("还未加入班级，无需解散！", "error");
                router.push({path: "home"});
                return false;
            }
            return true;
        },
        isClassAdmin() {
            let isClassAdmin = localStorage.getItem("userInfo") ? JSON.parse(localStorage.getItem("userInfo")).isClassAdmin : "";
            if (isClassAdmin !== "是") {
                prompt("非班级管理员，权限不足！", "error");
                router.push({path: "home"});
                return false;
            }
            return true;
        },
        loadClassData() {
            let token = localStorage.getItem("token") ? localStorage.getItem("token") : "";
            if (token === "") {
                prompt("还未登录！", "error");
                router.push({path: "/login"});
                return;
            }
            request.get("/user/adminClassInfo", {
                params: {
                    token: token
                }
            }).then(result => {
                //请求失败
                if (result.code !== "200") {
                    prompt(result.message, result.type);
                    return;
                }
                //请求成功
                this.userClassInfo = result.data.adminClassInfo[0];
            })
        },
        //解散班级
        dismissClass() {
            this.dismissClassData.classID = this.userClassInfo.classID;
            let className = this.userClassInfo.className;
            this.dismissClassData.userToken = localStorage.getItem("token") ? localStorage.getItem("token") : "";
            request.post(
                "/class/disMiss",
                this.dismissClassData
            ).then(result => {
                if (result.code != "200") {
                    prompt(result.message, result.type);
                    return;
                }
                prompt("成功解散" + className, result.type);
                router.push({path: "home"});
                return;
            });
        }
    }
}
</script>